package MuleBackend;

import java.awt.Point;
import javax.swing.ImageIcon;

/**
 * This class will be interacting with different
 * types of plane classes (methods). This class 
 * will set the resources available according to
 * the type of lands.
 * This class will keep track of its owner and 
 * the color representing the owner.
 */
public class Land {
	protected int food;
	protected int energy;
	protected int smithore;
	protected int crystite;
	protected int price;
	protected Player owner;
	protected int factory;
	protected String landType;
	protected ImageIcon image;
	protected Point	location = new Point(-1, -1);;
	public static final int noFactory=0;
	public static final int foodFactory=1;
	public static final int energyFactory=2;
	public static final int smithoreFactory=3;
	public static final int crystiteFactory=4;
	
	
	public Land() {
		//empty
	}
	//0 = town
	//1 = mountain1
	//2 = mountain2
	//3 = mountain3
	//4 = plains
	//5 = river
	/**
	 * constructor for the land class
	 * @param num the type of land it is
	 */
	public Land(int num){
		image = null;
		food = 0;
		energy = 0;
		smithore = 0;
		owner = null;
		factory = 0;
		price = 100;
		landType = "";
		initializeLand(num, this);
	}
	
	public Land(int food, int energy, int smithore) {
		this.food = food;
		this.energy = energy;
		this.smithore = smithore;
		owner = null;
		factory = 0;
		price = 100;
		landType = "";
	}
	
	/**
	 * 0 for no factory
	 * 1 for food factory
	 * 2 for energy factory
	 * 3 for smithore factory
	 * 4 for crystite factory
	 * @param i from 0-4
	 */
	public void setFactory(int i){
		factory = i;
		System.out.println("Factory have been set to "+factory);
	}
	
	/**
	 * @return integer from 0-4
	 */
	public int getFactory() { return factory; }
	
	public int produce(){
		int i=0;
		switch (factory){
			case 0:
				break;
			case 1:
				i = food;
				break;
			case 2:
				i = energy;
				break;
			case 3:
				i = smithore;
				break;
			case 4:
				i = crystite;
				break;
		}
		return i;
	}
	
	/**
	 * returns a string representation of the land
	 */
	public String toString(){
		return "Land Type is :" + landType +"\n" +
				"And Location is at \n" + 
				"Row :" + location.x + "\n" +
				"Column :" + location.y + "\n";
	}
	
	/**
	 * setter method to set the location of the land
	 * @param x
	 * @param y
	 */
	public void setLocation(int x, int y){
		location.x = x;
		location.y = y;
	}
	
	/**
	 * get method for x
	 * @return the x location
	 */
	public int getX(){return location.x;}
	
	/**
	 * get method for y
	 * @return the y location
	 */
	public int getY(){return location.y;}
	/**
	 * get method for the point that holds x and y
	 * @return the Point location
	 */
	public Point getLocation(){return location;}

	/**
	 * helper method to initialize the
	 * land to the right values
	 * @param num the type of land it is
	 * @param land the land object to modify
	 */
	private void initializeLand(int num, Land land){
		switch (num) {
			case 0:	initializeToTown(num, land); 		break;
			case 1:	initializeToMountain1(num, land);	break;
			case 2:	initializeToMountain2(num, land);	break;
			case 3:	initializeToMountain3(num, land);	break;
			case 4:	initializeToPlains(num, land);	break;
			case 5: initializeToRiver(num, land); 	break;
		}	
	}
	
	/**
	 * helper method to set the resources of the land
	 * @param food the food the land holds
	 * @param energy the energy the land holds
	 * @param smithore the smithore the land holds
	 */
	private void initializeResources(int food, int energy, int smithore){
		this.food = food;
		this.energy = energy;
		this.smithore = smithore;
	}
	
	/**
	 * helper method to set the right image to the right land
	 * @param num the land type
	 */
	private void initializeImage(int num){
		switch (num){
		case 0:	image = new ImageIcon("src/ShittyPackage/town.png");
				break;
		case 1:	image = new ImageIcon("src/ShittyPackage/mountain1.png");
				break;
		case 2:	image = new ImageIcon("src/ShittyPackage/mountain2.png");
				break;
		case 3:	image = new ImageIcon("src/ShittyPackage/mountain3.png");
				break;
		case 4:	image = new ImageIcon("src/ShittyPackage/plain.png");
				break;
		case 5: image = new ImageIcon("src/ShittyPackage/waterBoard.png");
				break;
		}
	}
	
	/**
	 * helper method to instantiate town values into land
	 * owner = -1 implies town can't be owned by anyone
	 * @param num the land type
	 * @param land the land object
	 */
	private void initializeToTown(int num, Land land){
		owner = null;
		land.initializeImage(num);
		land.initializeLandType("Town");
	}
	
	/**
	 * helper method to instantiate mountain1 values into land
	 * @param num the land type
	 * @param land the land object
	 */
	private void initializeToMountain1(int num, Land land){
		land.initializeImage(num);
		land.initializeResources(1, 1, 2);
		land.initializeLandType("Mountain1");
	}
	
	/**
	 * helper method to instantiate mountain2 values into land
	 * @param num the land type
	 * @param land the land object
	 */
	private void initializeToMountain2(int num, Land land){
		land.initializeImage(num);
		land.initializeResources(1, 1, 3);
		land.initializeLandType("Mountain2");
	}
	
	/**
	 * helper method to instantiate mountain3 values into land
	 * @param num the land type
	 * @param land the land object
	 */
	private void initializeToMountain3(int num, Land land){
		land.initializeImage(num);
		land.initializeResources(1, 1, 4);
		land.initializeLandType("Mountain3");
	}
	
	/**
	 * helper method to instantiate plains values into land
	 * @param num the land type
	 * @param land the land object
	 */
	private void initializeToPlains(int num, Land land){
		land.initializeImage(num);
		land.initializeResources(2, 3, 1);
		land.initializeLandType("Plains");
	}
	
	/**
	 * helper method to instantiate river values into land
	 * @param num the land type
	 * @param land the land object
	 */
	private void initializeToRiver(int num, Land land){
		land.initializeImage(num);
		land.initializeResources(4, 2, 0);
		land.initializeLandType("River");
	}

	/**
	 * helper method to set the type of the land
	 * @param landType the type of land
	 */
	private void initializeLandType(String landType){
		this.landType = landType;
	}
	
	/**
	 * if the passed in parameter is lower than the land's current crystite rating, nothing happens.
	 * otherwise, it is raised to that parameter
	 * @param crystite
	 */
	public void raiseCrystiteTo(int crystite){
		this.crystite= this.crystite>=crystite ? this.crystite : crystite;
	}

	/**
	 * this method return the integer value of crystite resources
	 */
	public ImageIcon getCrystiteImage(){
		if (crystite == 0)return new ImageIcon("src/ShittyPackage/crystite0.png");
		if (crystite == 1)return new ImageIcon("src/ShittyPackage/crystite1.png");
		if (crystite == 2)return new ImageIcon("src/ShittyPackage/crystite2.png");
		if (crystite == 3)return new ImageIcon("src/ShittyPackage/crystite3.png");
		return null;
	}
	
	/**
	 * raises the crystite level of the land
	 */
	public void addCrystite(){
		crystite++;
	}
	
	/**
	 * getter method to return the image
	 * @return the image of the land
	 */
	public ImageIcon getImage() { return this.image; }
	
	/**
	 * getter method to return the owner
	 * @return the owner of the land
	 */
	public Player getOwner() { return this.owner; }
	
	/**
	 * setter method to set the owner of the land
	 * @param owner the owner of the land
	 */
	public void setOwner(Player owner) { this.owner = owner; }
	
	/**
	 * getter method to get the price of the land
	 * @return value of the land
	 */
	public int getPrice() { return this.price; }
	
	/**
	 * getter method to get the minimum price of the land
	 * @return value of the land divided by 2
	 */
	public int getMinPrice() { return this.price/2; }
	
	/**
	 * getter method to get the maximum price of the land
	 * @return value of the land multiplied by 2
	 */
	public int getMaxPrice() { return this.price*2; }
	/**
	 * Returns the row position of the land in 2d array board
	 * @return row
	 */
	public int getRow(){ return location.x-1; }
	
	/**
	 * Returns the column position of the land in 2d array board
	 * @return column
	 */
	public int getCol(){ return location.y-1; }
	
}